<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------


/**
 * Upload_model
 *
 * Model used to save data from the upload form to the database
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Upload_model extends Base_model {
	function Upload_model( ) {
		parent::Base_model( );
	}
	
	function insertFolder( $data = array( ) ){
		$this->db->insert( 'media_folder', $data );
	}
	
	function getFolder( $folder_id = "" ){
		$this->db->select( 'media_folder.id,
							media_folder.title,
							media_folder.folder'
						   );
		$this->db->from( "media_folder" );
		$this->db->where( 'media_folder.parent', $folder_id );
		$this->db->where( "`media_folder`.`website_id`", $this->session->userdata( 'website_id' ) );
		$this->db->order_by( "media_folder.title", "ASC");
		return $this->db->get( );
	}
	
	function getFolderById( $folder_id ){
		if( $folder_id === '0' ){
			return '';
		}
		$this->db->select( 'media_folder.folder, media_folder.parent' );
		$this->db->from( "media_folder" );
		$this->db->where( 'media_folder.id', $folder_id );
		$folder = $this->db->get( );
		$row = $folder->result_array( );
		$folder_name = '';
		if( $row[0]['parent'] != 0 ){
			$folder_name .= $this->getFolderById( $row[0]['parent'] );
		} 
		$folder_name .= "/".$row[0]['folder']; 
		return $folder_name;
	}
	
	function getFolderTitleById( $folder_id ){
		if( $folder_id === 0 ){
			return '';
		}
		$this->db->select( 'media_folder.title' );
		$this->db->from( "media_folder" );
		$this->db->where( 'media_folder.id', $folder_id );
		$folder = $this->db->get( );
		$row = $folder->result_array( );
	
		return $row[0]['title'];
	}
	
	function insertFiles( $data = array( ) ){
		$this->db->insert( 'media_files', $data );
	}
	
	function getFiles( $folder_id ){
		$this->db->select( 'media_files.id,
							media_files.title,
							media_files.description,
							media_files.name'
						   );
		$this->db->from( "media_files" );
		$this->db->where( 'media_files.folder_id', $folder_id );
		$this->db->order_by( "media_files.title", "ASC");
		
		return $this->db->get( );
	}
	
	/**
	 * Delete folder with all files within it
	 *
	 * @access public
	 * @param int $folder_id Folder ID
	 */
	function delete( $folder_id ){
		//get the folder id and all file ids within this folder
		$this->db->select( 'media_files.id,
							media_files.name,
							media_files.file' );
		$this->db->from( "media_files" );
		$this->db->where( 'media_files.folder_id', $folder_id );
		$files = $this->db->get( );
		foreach( $files->result_array( ) as $row ){
			//use $this->delete_file for deleting all files in this folder
			$this->delete_file( $row['id'] );
		}
		//delete folder from the filesystem
    	$website = $this->session->userdata( 'template' );
    	$folder_name = '';  
    	if( $folder_id != 0 ){
    		$folder_name = $this->getFolderById( $folder_id ) . '/';
    	}
    	//@todo: needs to check if folder are in it!!! -> at the moment we can only delete the last folder in the directory structure
        $path = PREPATH.DEVPATH.'uploads/' . $website . $folder_name; //server directory
		if( rmdir( $path ) ){
			//if this is true, delete folder information in the database
			$this->db->delete( 'media_folder', array( 'id' => $folder_id ) );
		}
	}
	
	/**
	 * Delete file
	 *
	 * @access public
	 * @param int $file_id File ID
	 */
	function delete_file( $file_id ){
		//get actual path of the file
		$this->db->select( 'media_files.folder_id,
							media_files.name,
							media_files.file' );
		$this->db->from( "media_files" );
		$this->db->where( 'media_files.id', $file_id );
		$folder = $this->db->get( );
		$row = $folder->result_array( );
		$folder_id = $row[0]['folder_id'];
    	$website = $this->session->userdata( 'template' );
    	$folder_name = '';  
    	if( $folder_id != 0 ){
    		$folder_name = $this->getFolderById( $folder_id ) . '/';
    	}
        $path = PREPATH.DEVPATH.'uploads/' . $website . $folder_name; //server directory
        //delete image from directory path
		if( file_exists( $row[0]['file'] ) ){
			//if file is deleted on the filesystem...
			if( unlink( $row[0]['file'] ) ){
				//... it can be deleted from the image database
				$this->db->delete( 'media_files', array( 'id' => $file_id ) );
			}
		} 
	}
}
?>
